home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PCGUIA 117
/
PC Guia 117.iso
/
Software
/
Utils
/
Software6
/
Product13
/
googlebar-0.9.5.06-fx.xpi
/
chrome
/
googlebar.jar
/
content
/
googlebarUtil.js
< prev
next >
Wrap
Text File
|
2005-02-21
|
7KB
|
243 lines
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Googlebar for Mozilla.
*
* The Initial Developer of the Original Code is Andy Edmonds.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Andy Boughton
* Andrew Houghton
* John Woods
* Bernd Kuemmerlen
* Robert Mulcahy
* Alfred Kayser
* Reflex
* Gary Turnbull
* Raj Bhaskar
* Joachim Thewes
* Henrik Gemal
* Robert Fernandes
* Joe Chellman
* Franki Cheung
* Alban Fonrouge
* Martin Hassman
* Ufuk Kayserilioglu
* Yoni Gilad
* Tim Schmidt
* timeless
* Francis Turner
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
const GB_CONSOLE_SVC = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
const GB_DEBUG_DUMP = false; // Enable to see dump output - see logMessage function
const GB_DEBUG_MSG = true; // Enable to see logs in js console message secion - see logMessage function
var myGooglebarUtil = {
/*** Variable Declarations ***/
mInFirefox: false,
mInPrefs: false,
/*** Function Declarations ***/
buildUrl: function(aPathArray, aOffset) {
var mystr = '';
for (var i = 0; i < aOffset; i++) {
if (aPathArray[i].length) {
mystr += aPathArray[i];
mystr += "/";
if (i < (aOffset - 1)) {
if (i == 0) {
if (aPathArray[i] == "file:") {
mystr += "/";
}
// if it's the first item (protocol) then add a second /
if (i == 0) {
mystr += "/";
}
}
}
}
}
return mystr;
},
/*
A convenience function to get googlebar elements regardless of where
the function is being called from within the googlebar framework
(toolbar, popups, dialogs, prefs, etc.) Uses the inPrefs flag to
attempt to find the element from opener first, otherwise it attempts
to first use the current document.
*/
getAnonymousElement: function(aParent, aAttr, aValue) {
if (!aParent || !aAttr || !aValue) { return null; }
var doc, element;
try {
doc = this.isInPrefs() ? top.opener.document : document;
element = doc.getAnonymousElementByAttribute(aParent, aAttr, aValue);
}
catch (e) {
element = null;
}
if (element != null) { return element; }
try {
doc = this.isInPrefs() ? document : top.opener.document;
element = doc.getAnonymousElementByAttribute(aParent, aAttr, aValue);
}
catch (e) {
element = null;
}
return element;
},
/*
A convenience function to get googlebar elements regardless of where
the function is being called from within the googlebar framework
(toolbar, popups, dialogs, prefs, etc.) Use the inPrefs flag to
attempt to find the element from opener first, otherwise it attempts
to first use the current document.
*/
getElement: function(aId) {
if (!aId) { return null; }
var doc, element;
try {
doc = this.isInPrefs() ? top.opener.document : document;
element = doc.getElementById(aId);
}
catch (e) {
element = null;
}
if (element != null) { return element; }
try {
doc = this.isInPrefs() ? document : top.opener.document;
element = doc.getElementById(aId);
}
catch (e) {
element = null;
}
return element;
},
/*
A convenience method which uses the getElement function
to retrieve an element from anywhere within the googlebar
framework (toolbar, popups, dialogs, prefs, etc.) and
get an attribute value from that element.
*/
getAttribute: function(aId, aAttr) {
if (!aId || !aAttr) { return null; }
var element = this.getElement(aId);
if (element == null) { return null; }
return element.getAttribute(aAttr);
},
inArray: function(aArray, aString) {
for (var i = 0; i < aArray.length; i++) {
if (aArray[i].toLowerCase() == aString.toLowerCase()) {
return true;
}
}
return false;
},
initialize: function() {
// TODO: Someday find a real way to do this besides looking for a null broadcaster
this.mInFirefox = this.getElement('googlebar_contextpref') == null;
},
isInFirefox: function() {
return this.mInFirefox;
},
isInPrefs: function() {
return this.mInPrefs;
},
logMessage: function(aMessage) {
if (GB_DEBUG_DUMP) { dump('Googlebar: ' + aMessage); }
if (GB_DEBUG_MSG) { GB_CONSOLE_SVC.logStringMessage('Googlebar: ' + aMessage); }
},
normalizeString: function(aString) {
// trim leading and trailing spaces
var newString = this.trimString(aString);
// reduce runs of more than one space to one space only
newString = newString.replace(/(\s+)/g, ' '); // a space
return newString;
},
setInPrefs: function(aBoolean) {
if (aBoolean == true || aBoolean == "true") {
this.mInPrefs = true;
}
else {
this.mInPrefs = false;
}
},
trimString: function(aString) {
if (!aString) {
return "";
}
return aString.replace(/(^\s+)|(\s+$)/g, '');
},
updateClass: function (aElement, aPosition, aNewClass) {
var oldClass = aElement.getAttribute("class");
var classes = oldClass.split(" ");
classes[aPosition] = aNewClass;
aElement.setAttribute("class", classes.join(" "));
}
};
myGooglebarUtil.initialize();